from typing import List,Union,Optional,Dict
from nicegui import ui,binding
from nicegui.element import Element
from enums import OptionEnums    
from search_able_select import SearchAbleSelect as searchSelect


class DiseaseElement():
    diseases=binding.BindableProperty()
    __selections=binding.BindableProperty()
    def __init__(self,diseases:List[str]=[],type:str='disease',columns:int=4) -> None:
        self.__columns = len(diseases) if diseases else columns
        self.diseases =  ['' for i in range(self.__columns)] if not diseases else diseases
        # self.diseases = {i:'' for i in range(self.__columns)} if not diseases else {i:item for i,item in enumerate(diseases)}
        # self.__selections={i:False for i in range(self.__columns)}
        self._type = type
        self._label='手术或操作' if type=='surgery' else '诊断'   # surgery表示手术,disease表示疾病
        # self.__selected_disease_index=None
        # 用于存储当前被选中的SearchAbleSelect对象,用于实现同一时间仅有个SearchAbleSelect对象被选中。list是引用类型，可以实现在调用函数中修改值
        self.__selected_obj=[0]
        # 将所有SearchAbleSelect对象存储在列表中，方便实现上下移动、删除等功能
        self.search_select_list=[]
        self.__grid_contains=ui.row()
        self.__add_element()


    def __add_element(self):
         with self.__grid_contains:
            with ui.grid(columns='auto '*4).classes('w-full text-center').style('gap:0px'):
                for index,value in enumerate(self.diseases):
                    label_name=f'主要{self._label}' if index==0 else f'其他{self._label}{index}'
                    # options=[value] if value else None
                    _select=searchSelect(value,label=label_name,disease_or_surgery=self._type,selected_obj=self.__selected_obj,options=[value])
                    _select.value=value if value else None
                    self.search_select_list.append(_select)


    


    def __move(self,offset:int)->None:
        if self.__selected_obj is None or self.__selected_obj[0] ==0:
            return
        index=self.search_select_list.index(self.__selected_obj[0])
        options=self.search_select_list[index+offset].options
        value=self.search_select_list[index+offset].value
        self.search_select_list[index+offset].set_options(self.__selected_obj[0].options)
        self.search_select_list[index+offset].value=self.__selected_obj[0].value
        self.__selected_obj[0].set_options(options)
        self.__selected_obj[0].value=value
        self.search_select_list[index+offset].is_selected,self.__selected_obj[0].is_selected=self.__selected_obj[0].is_selected,self.search_select_list[index+offset].is_selected
        self.diseases[index+offset],self.diseases[index]=self.diseases[index],self.diseases[index+offset]
        self.__selected_obj[0]=self.search_select_list[index+offset]


    def move_pre(self):  
        if not self.__selected_obj or self.__selected_obj[0]==0:
            return 
        # ui.notify(f'move_pre {self.__selected_obj[0]}')      
        if self.search_select_list.index(self.__selected_obj[0])==0:          
            return
        self.__move(-1)


    def move_next(self):
        if not self.__selected_obj or self.__selected_obj[0]==0: 
            return
        if self.search_select_list.index(self.__selected_obj[0])==len(self.search_select_list)-1:
            return 
        self.__move(1)


    def delete(self):
        if self.__selected_obj is None or self.__selected_obj[0]==0:
            return
        if isinstance(self.__selected_obj[0],searchSelect):
            index=self.search_select_list.index(self.__selected_obj[0])
            for i in range(index,len(self.search_select_list)-1):
                self.search_select_list[i].value=self.search_select_list[i+1].value
                self.search_select_list[i].is_selected=self.search_select_list[i+1].is_selected
            self.search_select_list[len(self.search_select_list)-1].value=''
            self.search_select_list[len(self.search_select_list)-1].is_selected=False
            self.__selected_obj[0]=0
    
    def add(self):
        if self.__columns>=12:
              return
        self.diseases.append('')
        self.__columns+=1
        self.__grid_contains.clear()
        self.__add_element()


    def set_diseases(self,diseases:List[str])->None:
        if not diseases:
            return
        self.diseases=diseases
        self.__columns=len(diseases)
        self.__selected_obj[0]=0
        self.search_select_list.clear()
        self.__grid_contains.clear()
        self.__add_element()


    def get_diseases(self)->List[str]:
        return [(item.value).split(' ')[0] for item in self.search_select_list if  item.value]